package com.gec.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.entity.Doctors;
import com.gec.service.DoctorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author GOF
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/doctors")
public class DoctorsController {

    @Autowired
    private DoctorsService doctorsService;

    @RequestMapping("/list")
    public String showList(Model model){
        //方法带2的是联表查询,不带2的是单表查询,按需使用
        List<Doctors> doctors = doctorsService.list2();
        model.addAttribute("doctors",doctors);
        return "doc_list";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(String uname, String pwd, HttpSession session, Model model) {
        QueryWrapper<Doctors> qw = new QueryWrapper<>();
        //将字段与条件绑定
        qw.lambda().eq(Doctors::getName, uname).eq(Doctors::getPwd, pwd);

        Doctors doctor = doctorsService.getOne(qw);
        if (doctor != null) {
            session.setAttribute("doctor", doctor);
            return "redirect:/jobs/list";
        }
        model.addAttribute("err", "用户名或密码错误");
        return "login";
    }
}
